package service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Additional methods for converting data into needed format/type
 */
public class Converter {

	public static Date intoDate(String timeText) {
		if (isEmpty(timeText)) {
			return null;
		}
		SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
		Date answerTime = new Date();
		try {
			answerTime = format.parse(timeText);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return answerTime;
	}

	public static java.sql.Date getSqlDate(java.util.Date date) {
		return date != null ? new java.sql.Date(date.getTime()) : null;
	}

	public static boolean isNotEmpty(String string) {
		return string != null && !string.isEmpty() && !string.trim().isEmpty();
	}

	public static boolean isEmpty(String string) {
		return !isNotEmpty(string);
	}

	public static Double formatDouble(Double value, int scale) {
		return new BigDecimal(value).setScale(scale, RoundingMode.HALF_UP).doubleValue();
	}
}
